﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class QuarterPlan_ModifyEvaluate : System.Web.UI.Page
{
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGridViewCheckNote();
        }
    }

    /// <summary>
    /// 绑定待评估GridView
    /// </summary>
    void bindGridViewCheckNote()
    {
        object[] _value = new object[1];
        _value[0] = Utility.HttpHelp.getParamsInt("QuarterPlanId");
        DataTable _quarterPlan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
        this.gdvEvaluateCheckNote.DataSource = _quarterPlan;
        this.gdvEvaluateCheckNote.DataBind();
        #region 截取字符串
        for (int i = 0; i < this.gdvEvaluateCheckNote.Rows.Count; i++)
        {
            DropDownList drp = (DropDownList)this.gdvEvaluateCheckNote.Rows[i].Cells[11].FindControl("dropZhuangTai");
            drp.SelectedValue = this.gdvEvaluateCheckNote.DataKeys[i]["PlanSendState"].ToString();
            #region 查看年计划分解
            object[] _valueQuarterPlanResolve = new object[1];
            _valueQuarterPlanResolve[0] = this.gdvEvaluateCheckNote.DataKeys[i]["QuarterPlanId"].ToString();
            DataTable _quarterResolve = dllQuarterPlanDetail.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _valueQuarterPlanResolve);
            if (_quarterResolve.Rows.Count > 0)
            {
                Button btn = (Button)this.gdvEvaluateCheckNote.Rows[i].Cells[9].FindControl("btnSelectResolvQuarterPlan");
                Label lbl = (Label)this.gdvEvaluateCheckNote.Rows[i].Cells[9].FindControl("lblTopic");
                btn.Visible = true;
                lbl.Visible = false;
            }
            else
            {
                Button btn = (Button)this.gdvEvaluateCheckNote.Rows[i].Cells[9].FindControl("btnSelectResolvQuarterPlan");
                Label lbl = (Label)this.gdvEvaluateCheckNote.Rows[i].Cells[9].FindControl("lblTopic");
                btn.Visible = false;
                lbl.Visible = true;
            }
            wrapByComma(this.gdvEvaluateCheckNote, i, 6);
            wrapByComma(this.gdvEvaluateCheckNote, i, 7);
            if (this.gdvEvaluateCheckNote.Rows[i].Cells[10].Text == "&nbsp;" || this.gdvEvaluateCheckNote.Rows[i].Cells[10].Text == "&amp;nbsp;")
            {
                this.gdvEvaluateCheckNote.Rows[i].Cells[10].Text = "";
            }
            #endregion
            if (this.gdvEvaluateCheckNote.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gdvEvaluateCheckNote.Rows[i].Cells[12].FindControl("btnDownLoad1");
                _btn.Visible = false;
                Label _lbl = (Label)this.gdvEvaluateCheckNote.Rows[i].Cells[12].FindControl("lblAlert1");
                _lbl.Visible = true;
                _lbl.Text = "无附件";
            }
        }
        #endregion

    }


    /// <summary>
    /// 评估表中的下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad1_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列

        string path = this.gdvEvaluateCheckNote.DataKeys[i]["AttachmentPath"].ToString();
        // Response.Redirect("~/upload/"+path);
        FileDownload(path);
    }


    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }


   


    /// <summary>
    /// 查看年分解计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-25</date>
    protected void btnSelectResolvQuarterPlan_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        int _quarterPlanId = Convert.ToInt32(this.gdvEvaluateCheckNote.DataKeys[i]["QuarterPlanId"].ToString());
        string _url = "QuarterPlanResolveDetail.aspx?_quarterPlanId=" + _quarterPlanId;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=350,width=900,top=200,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");


    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }


    /// <summary>
    /// 是否达成共识提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTiJiao_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        DropDownList drp = (DropDownList)this.gdvEvaluateCheckNote.Rows[i].Cells[11].FindControl("dropZhuangTai");
        ListItem _li = drp.SelectedItem;

        if (_li != null)
        {
            if (_li.Text != "请选择")
            {
                object[] _value = new object[3];
                _value[0] = Convert.ToInt32(this.gdvEvaluateCheckNote.DataKeys[i]["QuarterPlanId"].ToString());
                TextBox tb = (TextBox)this.gdvEvaluateCheckNote.Rows[i].Cells[11].FindControl("txtCheckNote");
                if (tb.Text.Length < 18)
                {
                    Utility.HttpHelp.alert("评估意见不得少于32个字符", Page);
                    return;
                }
                tb.Text = tb.Text + "\r\n";
                _value[1] = tb.Text;
                _value[2] = Convert.ToInt32(_li.Value);
                //填写checknote字段内容（追加）
                int result = dllQuarterPlanofManager.edit(QuarterPlanofManager.field.CheckNote.ToString(), QuarterPlanofManager.field.PlanSendState.ToString(), _value);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("提交评估意见未成功，请重试！");
                }
                else
                {
                    string _department = Server.UrlEncode(Utility.HttpHelp.getParamsString("department"));
                    Utility.HttpHelp.closeWindow("QuarterPlanDisplay.aspx?department=" + _department + "&Id=" + Utility.HttpHelp.getParamsInt("Date"), Page);
                }
            }
            else
            {
                Utility.HttpHelp.alert("请选择计划完成状态", Page);
            }
        }
    }


    /// <summary>
    /// 查看年分解计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-25</date>
    protected void btnSelectResolvQuarterPlanEvaluate_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        int _quarterPlanId = Convert.ToInt32(this.gdvEvaluateCheckNote.DataKeys[i]["QuarterPlanId"].ToString());
        string _url = "QuarterPlanResolveDetail.aspx?_quarterPlanId=" + _quarterPlanId;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=350,width=900,top=200,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");


    }
}
